package webmarket.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import webmarket.common.DataTablesParamUtility;
import webmarket.common.DatatableResponse;
import webmarket.controller.transferobjects.JQueryDataTableParamModel;
import webmarket.controller.transferobjects.UserTO;
import webmarket.jpacontrollers.UserJpaController;
import webmarket.model.User;

@Controller
@RequestMapping("/administration")
public class AdministrationController extends ControllerBase {

	@RequestMapping("/users")
	public ModelAndView administration_users(ModelMap model) {
		return new ModelAndView("administration_users", model);
	}

	@RequestMapping(value = "/users", method = RequestMethod.POST)
	public @ResponseBody
	DatatableResponse<List<UserTO>> administration_users_post(HttpServletRequest request) {
		JQueryDataTableParamModel param = DataTablesParamUtility.getParam(request);
		DatatableResponse<List<UserTO>> response = new DatatableResponse<List<UserTO>>(param);
		String userName = param.sSearch;
		String sortCol = param.getSortColumnName();
		String sortDir = param.sSortDirection;
		int limit = param.iDisplayLength;
		int offset = param.iDisplayStart;
		UserJpaController ujc = new UserJpaController();
		List<User> data = ujc.getUsers(userName, sortCol, sortDir, limit, offset);
		response.setAaData(UserTO.convert(data));
		response.setiTotalDisplayRecords(new Integer(ujc.getCount(userName)).toString());
		response.setiTotalRecords(new Integer(ujc.getCount()).toString());
		return response;
	}
}
